package com.vae.content.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vae.content.domain.dto.BindTeachplanMediaDto;
import com.vae.content.domain.dto.SaveTeachplanDto;
import com.vae.content.domain.dto.TeachplanDto;
import com.vae.content.domain.entity.Teachplan;
import com.vae.content.domain.entity.TeachplanMedia;
import com.vae.content.service.TeachplanMediaService;
import com.vae.content.service.TeachplanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: vae
 * 功能: 课程计划接口
 * 时间: 2024-04-07 20:55
 */
@RefreshScope
@Api(value = "课程计划接口",tags = "课程计划接口")
@RestController
public class TeachplanController {

    @Autowired
    TeachplanService teachplanService;

    @Autowired
    TeachplanMediaService teachplanMediaService;
    /**
     * 课程计划查询
     * @param courseId
     * @return
     */
    @ApiOperation("课程计划查询")
    @ApiImplicitParam(value = "courseId", name = "课程Id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public List<TeachplanDto> getTreeNodes(@PathVariable Long courseId){
        List<TeachplanDto> teachplanTree = teachplanService.findTeachplanTree(courseId);
        return teachplanTree;
    }

    /**
     * 课程计划创建|修改
     * @param teachplan
     */
    @ApiOperation("课程计划的创建|修改")
    @PostMapping("/teachplan")
    public void saveTeachplan( @RequestBody SaveTeachplanDto teachplan){
        teachplanService.saveTeachplan(teachplan);
    }

    /**
     * 课程计划删除
     * @param teachplanId
     */
    @ApiOperation("课程计划删除")
    @DeleteMapping("/teachplan/{teachplanId}")
    public void deleteTeachplan(@PathVariable Long teachplanId){
        teachplanService.deleteTeachplan(teachplanId);
    }

    /**
     * 课程计划层级下移
     * @param teachplanId
     */
    @ApiOperation("课程计划层级下移")
    @PostMapping("/teachplan/movedown/{teachplanId}")
    public void movedownTeachplan( @PathVariable Long teachplanId){
        teachplanService.movedownTeachplan(teachplanId);
    }

    /**
     * 课程计划层级上移
     * @param teachplanId
     */
    @ApiOperation("课程计划层级上移")
    @PostMapping("/teachplan/moveup/{teachplanId}")
    public void moveupTeachplan( @PathVariable Long teachplanId){
        teachplanService.moveupTeachplan(teachplanId);
    }

    /**
     * 课程绑定视频
     * @param bindTeachplanMediaDto
     */
    @ApiOperation(value = "课程绑定视频")
    @PostMapping("/teachplan/association/media")
    void associationMedia(@RequestBody BindTeachplanMediaDto bindTeachplanMediaDto){
        teachplanService.associationMedia(bindTeachplanMediaDto);
    }


    /**
     * 删除课程绑定的视频
     * @param teachplanId
     * @param mediaId
     */
    @ApiOperation(value = "删除课程绑定的视频")
    @DeleteMapping("/teachplan/association/media/{teachplanId}/{mediaId}")
    void moveAssociationMedia(@PathVariable String teachplanId, @PathVariable String mediaId){
        LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeachplanMedia::getTeachplanId,teachplanId);
        wrapper.eq(TeachplanMedia::getMediaId,mediaId);
        teachplanMediaService.remove(wrapper);
    }

    @ApiOperation(value = "课程计划的视频是否可以试看")
    @ResponseBody
    @GetMapping("/r/getTeachplanIsPreview/{teachplanId}")
    public Boolean getTeachplanIsPreview(@PathVariable("teachplanId") Long teachplanId) {
        Teachplan teachplan = teachplanService.getById(teachplanId);
        String isPreview = teachplan.getIsPreview();
        if (isPreview.equals("1")) {
            //可以预览/试学
            return Boolean.TRUE;
        } else {
            //不可以预览/试学
            return Boolean.FALSE;
        }
    }

}
